<template src="./template.html"></template>

<style src="./style.css" scoped></style>

<script>
    import shopAllIcon from 'assets/images/shop-all-icon.png';
    import rightArrowWhiteImg from 'assets/images/right-icon-white.png';
    import api from 'api/api';
    import modal from 'utils/modal';
    import navigator from 'utils/navigator';
    import route from 'constants/route';
    import constant from 'constants/constant';
    import cartService from 'services/cartService';

    const globalEvent = weex.requireModule('globalEvent');

     /**解析读取的数据*/
    function vendorData(data, vm) {
       //初始化banner
        vm.imageList = data.flash || [];

        //初始化店铺
        vm.shopList = data.shop || [];

        //初始限时抢购对象
        let xsqg = data.xsqg || {};
        vm.xsqgList = xsqg.goods || [];
        vm.clockNow = xsqg.sys_time || null;
        vm.clockTarget = xsqg.end_time || null;

        //初始化精选专题对象
        vm.specialData = data.recipe_data || {};

        //初始化常购精选
        vm.cgjxList = data.common_goods || [];

        //初始化人气精选和人气精选商品
        vm.rqjxList = data.special || [];
        vm.rqjxGoodsList = data.special_goods || [];

        //热卖商品
        let hotGoods = data.hotgoods || [];
        if(hotGoods.length < vm.limit) {
            vm.listCompleted = true;
        }
        vm.hotGoodsList = hotGoods;
    }

    export default {
        data () {
            return {
                /*刷新图标参数*/
                refreshing: false,

                /*显示隐藏置顶按钮*/
                showGoTop: false,//默认置顶按钮为隐藏
                loading: false, //默认的隐藏加载提示

                shopAllIcon: shopAllIcon, //全部店铺
                rightArrowWhiteImg: rightArrowWhiteImg, //向右的箭头

                /*页面参数*/
                imageList: [],  //banner广告数组
                shopList: [], //店铺数组
                xsqgList: [],   //限时抢购数组
                cgjxList: [],   //常购精选数组
                rqjxList: [],   //人气精选数组
                rqjxGoodsList: [],   //人气精选商品数组
                hotGoodsList: [], //热卖商品
                specialData: {}, //精选专题对象

                clockNow: null,     //限时抢购当前服务器时间戳
                clockTarget: null,   //限时抢购结束服务器时间戳

                /*分页接口参数*/
                start: 0,
                limit: 9,
                listCompleted: false  //数据列表加载完成
            }
        },
        created() {
            let self = this;
            self.sendHttp(res=>{
                vendorData(res.data, self);
            });

            //同步购物车商品数量
            cartService.resumeCartGoods(self);

            //监听首页刷新事件机制
            globalEvent.addEventListener(constant.EVENT_REFRESH_HOME, function (e) {
                self.pageReload();
            });

        },
        methods: {
            /**发送Http请求*/
            sendHttp(success, fail) {
                let self = this;
                api.sendHttp('homeApi', {start: self.start, limit: self.limit}).then(
                    res =>{
                        if(typeof success === 'function') success(res);
                    }, res=>{
                        modal.toast(res.msg);
                        if(typeof fail === 'function') fail(res);
                });
            },

            /**无限滚动分页*/
            fetchMore() {
                this.$refs['list'].resetLoadmore(); //下一次滚动时强制触发loadmore

                let self = this;
                if(self.listCompleted || self.loading) {
                    return false;
                }
                self.loading = true;
                let temp = self.start + self.limit;
                self.start = temp;

                api.sendHttp('homeApi',{start: self.start, limit: self.limit}).then(
                    res => {
                        self.loading = false;
                        let data = res.data || {};
                        let _hotGoods = data.hotgoods || [];
                        if(_hotGoods.length < self.limit) self.listCompleted = true;
                        self.hotGoodsList = self.hotGoodsList.concat(_hotGoods);
                    }, res => {
                        self.loading = false;
                        modal.toast(res.msg);
                    }
                );
            },

            /**页面下拉刷新*/
            onRefresh (event) {
                let self = this;
                self.refreshing = true;
                self.pageReload();
                setTimeout(() => {
                    self.refreshing = false;
                }, 1000);
            },

            /**页面重载*/
            pageReload(){
                let self = this;
                //重置初始化数据
                self.start = 0;
                self.listCompleted = false;
                self.clockNow = null;
                self.clockTarget = null;

                //重置页面业务数据
                self.specialData = null; //食谱数据
                self.xsqgList.length = 0; //重置限时抢购数组
                self.cgjxList.length = 0; //常购精选数组
                self.rqjxList.length = 0;   //人气精选数组
                self.rqjxGoodsList.length = 0;   //人气精选商品数组
                self.hotGoodsList.length = 0; //热卖商品数组

                self.sendHttp(res=>{
                    vendorData(res.data, self);
                });
            },

            /**list滚动监听事件，动态的控制置顶按钮显示与隐藏*/
            onScroll(e) {
                let contentOffset = e.contentOffset;
                let offsetY = contentOffset.y;
                if(offsetY <= -4000){
                    if(!this.showGoTop) this.showGoTop = true;
                } else if(offsetY > -4000) {
                    if(this.showGoTop) this.showGoTop = false;
                }
            },

            /**限时抢购结束操作*/
            alarm() {
                let self = this;
                self.clockNow = null;
                self.clockTarget = null;
                self.xsqgList.length = 0;

                self.sendHttp(res=>{
                    let data = res.data || {};
                    let xsqg = data.xsqg || {};
                    self.xsqgList = xsqg.goods || [];
                    self.clockNow = xsqg.sys_time || null;
                    self.clockTarget = xsqg.end_time || null;
                });
            },

            /**广告位、人气精选跳转*/
            bannerClick(banner) {
                banner = banner || {};
                let options = banner.jumpData || {};
                navigator.push(options);
            },

            /**跳转店铺*/
            goToShop(shop){
                let options = {
                    url: route.shop_index,
                    title: shop.name,
                    params: {
                        shopType: shop.shoptype
                    }
                };
                navigator.push(options);
            },

            /**跳转全部店铺*/
            goToMoreShop(){
                let options = {
                    url: route.shop_all,
                    title: '更多店铺'
                };
                navigator.push(options);
            },

            /**跳转限时抢购*/
            goToTimeout(e) {
                let options = {
                   url: route.activity_timeout,
                   title: "限时抢购"
                };

                navigator.push(options, event => {
                    console.log('跳转限时抢购');
                });
            },

            /**跳转原生食谱*/
            goToRecipe(){
                //todo 跳转到原生食谱
            },
            getSliceList(list,end){
                list = list || [];
                end = end || 0;
                return list.slice(0,end);
            }
        },
        destroyed(){
            globalEvent.removeEventListener(constant.EVENT_REFRESH_HOME);
            cartService.removeResumeCartGoodsListener();
        }
    }
</script>
